﻿package com.pandemicode.framework.controls {
	import com.pandemicode.framework.utils.Disposer;
	import flash.display.Sprite;
	import flash.events.Event;
	import com.pandemicode.framework.interfaces.IDisposeable;
	import org.osflash.signals.natives.NativeSignal;
	
	/**
	* @author Michael Lawrence
	* @version 1.0.0
	*/
	public class StatusPanel extends Sprite implements IDisposeable {
		private var _frameRate:FrameRate;
		private var _memoryUsage:MemoryUsage;
		private var _traceWindow:TraceWindow;
		private var _padding:Number;
		private var _textColor:uint;
		private var _font:String;
		private var _embedFonts:Boolean;
		private var _backgroundColor:uint;
		private var _backgroundBorderColor:uint;
		private var _backgroundBorderWidth:Number;
		
		private var _addedToStage:NativeSignal;
		private var _removedFromStage:NativeSignal;
		private var _stageResized:NativeSignal;
		
		public function get frameRate():FrameRate { return this._frameRate; }
		public function get memoryUsage():MemoryUsage { return this._memoryUsage; }
		public function get traceWindow():TraceWindow { return this._traceWindow; }
		
		public function StatusPanel(padding:Number = 20, textColor:uint = 0xff000000, font:String = "Arial", embedFonts:Boolean = false, backgroundColor:uint = 0xffcccccc, backgroundBorderColor:uint = 0xff000000, backgroundBorderWidth:Number = 3) {
			this._padding = padding;
			this._textColor = textColor;
			this._font = font;
			this._embedFonts = embedFonts;
			this._backgroundColor = backgroundColor;
			this._backgroundBorderColor = backgroundBorderColor;
			this._backgroundBorderWidth = backgroundBorderWidth;
			
			this._addedToStage = new NativeSignal(this, Event.ADDED_TO_STAGE, Event);
			this._removedFromStage = new NativeSignal(this, Event.REMOVED_FROM_STAGE, Event);
		
			if (this.stage) this._init();
			else this._addedToStage.add(this._init);
			this._removedFromStage.add(this._dispose);
		}
		
		private function _init(e:Event = null):void {
			this._addedToStage.remove(this._init);
			
			this._stageResized = new NativeSignal(this.stage, Event.RESIZE, Event);
			this._frameRate = new FrameRate();
			this._memoryUsage = new MemoryUsage();
			this._traceWindow = new TraceWindow(10, this._textColor, this._font, this._embedFonts, 300, 100, this._backgroundColor, this._backgroundBorderColor, this._backgroundBorderWidth);
			
			this.addChild(this._frameRate);
			this.addChild(this._memoryUsage);
			this.addChild(this._traceWindow);
			
			this._frameRate.embedFonts =
			this._memoryUsage.embedFonts = this._embedFonts;
			
			this._memoryUsage.textColor =
			this._frameRate.textColor = this._textColor;
			
			this._frameRate.changed.add(this._updatePositions);
			this._memoryUsage.changed.add(this._updatePositions);
			this._stageResized.add(this._updatePositions);
			
			this._updatePositions();
		}
		
		private function _updatePositions(e:Event = null):void {
			if(this._frameRate != null) {
				this._frameRate.x = 0;
				this._frameRate.y = this.stage.stageHeight - this._frameRate.height;
			}
			
			if(this._memoryUsage != null) {
				this._memoryUsage.x = this._frameRate.x + +this._frameRate.width + this._padding;
				this._memoryUsage.y = this.stage.stageHeight - this._memoryUsage.height;
			}
			
			if(this._traceWindow != null) {
				this._traceWindow.x = this.stage.stageWidth - this._traceWindow.width;
				this._traceWindow.y = this.stage.stageHeight - this._traceWindow.height;
			}
		}
		
		public function dispose():void {
			this._dispose();
		}
		
		private function _dispose(e:Event = null):void {
			this._addedToStage.removeAll();
			this._removedFromStage.removeAll();
			
			if (this._stageResized) this._stageResized.removeAll();
			if (this._frameRate) this._frameRate.changed.removeAll();
			if (this._memoryUsage) this._memoryUsage.changed.removeAll();
			
			Disposer.dispose(this._frameRate, this._memoryUsage, this._traceWindow);
		}
	}
}
